package com.wewins.fota.controller;

import com.wewins.fota.bean.bo.filter.LogFilterCond;
import com.wewins.fota.bean.dto.ExportReq;
import com.wewins.fota.bean.dto.PageReq;
import com.wewins.fota.bean.dto.PageRst;
import com.wewins.fota.bean.dto.ResponseRst;
import com.wewins.fota.bean.po.FotaLog;
import com.wewins.fota.common.constant.AuthConst;
import com.wewins.fota.common.util.FileUtil;
import com.wewins.fota.service.FotaLogService;
import com.wewins.fota.sys.token.TokenManager;
import org.apache.commons.io.FileUtils;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

import static org.apache.tools.ant.launch.Locator.encodeURI;

@RestController
@RequestMapping("/log")
public class FotaLogController implements BasicController<FotaLog, LogFilterCond> {
    private final FotaLogService service;

    private TokenManager tokenManager;

    @Autowired
    public FotaLogController(FotaLogService service, TokenManager tokenManager) {
        this.service = service;
        this.tokenManager = tokenManager;
    }

    @Override
    @PostMapping("query")
    public ResponseRst<PageRst<FotaLog>> queryAll(@RequestBody PageReq<LogFilterCond> pageReq) {
        return ResponseRst.<PageRst<FotaLog>>builder()
                .data(service.queryAll(pageReq))
                .build().withOk();
    }

    @Override
    @GetMapping("/query/{id}")
    public ResponseRst<FotaLog> queryById(@PathVariable long id) {
        return ResponseRst.<FotaLog>builder()
                .data(service.findById(id))
                .build().withOk();
    }

    @Override
    @PostMapping("/insert")
    public ResponseRst<FotaLog> insert(HttpServletRequest request, @RequestBody FotaLog inputParam) {
        return ResponseRst.<FotaLog>builder()
                .data(service.insert(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), inputParam))
                .build().withOk();
    }

    @Override
    @Delete("/delete/{id}")
    public ResponseRst<Integer> deleteById(HttpServletRequest request, @PathVariable long id) {
        return ResponseRst.<Integer>builder()
                .data(service.deleteById(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), id))
                .build().withOk();
    }

    @Override
    @PutMapping("/update")
    public ResponseRst<Integer> update(HttpServletRequest request, @RequestBody FotaLog inputParam) {
        return ResponseRst.<Integer>builder()
                .data(service.update(this.tokenManager.getUsernameFromToken(request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION)), inputParam))
                .build().withOk();
    }

    @PostMapping("/export")
    public void export(@RequestBody ExportReq<LogFilterCond> cond, HttpServletResponse response) throws IOException {
        File file = service.export(cond);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/octet-stream");
            String fileName = encodeURI(file.getName());
            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\";filename*=utf-8''" + fileName);
            FileUtil.load(response, file);
        } finally {
            FileUtils.forceDelete(file);
        }
    }
}
